/*
 * Class
 * SettingsProvider
 */
package pl.abstractvoid.settings;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JOptionPane;
import pl.abstractvoid.languages.LanguageProvider;

/**
 * Provides the access to user settings which are stored in a property file.
 * 
 * @author Wojciech Szałapski
 * @author Ivo Zieliński
 */
public class SettingsProvider {

    /**
     * Error code of the situation when the settings file can not be created.
     */
    private static final int CANNOT_CREATE_SETTINGS_FILE = 1;
    /**
     * The only instance of the SettingProvider.
     */
    private static SettingsProvider instance;
    /**
     * Name of the settings configuration file.
     * <p>
     * Acutally a path relative to the folder with executable jar.
     */
    private final String propertyFileName = "settings.cfg";
    /**
     * Settings properties set.
     */
    private final Properties prop;

    /**
     * Private constructor to preserve the singleton pattern.
     */
    private SettingsProvider() {
        prop = new Properties();
        File propertyFile = new File(propertyFileName);
        if (!propertyFile.isFile()) {
            try {
                propertyFile.createNewFile();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, LanguageProvider.getInstance().getString("canNotCreateSettingsFile"), LanguageProvider.getInstance().getString("error"), JOptionPane.ERROR_MESSAGE);
                System.exit(CANNOT_CREATE_SETTINGS_FILE);
            }
        }
    }

    /**
     * Creates if needed and returns the only instance of the SettingsProvider.
     *
     * @return The only instance of the settings provider.
     */
    public static SettingsProvider getInstance() {
        if (instance == null) {
            instance = new SettingsProvider();
        }
        return instance;
    }

    /**
     * Sets the property value and stores it in the configuration file.
     *
     * @param propertyName Name of the property.
     * @param value Value of the property.
     * @throws IOException
     */
    public void setProperty(String propertyName, String value) throws IOException {
        try (FileOutputStream output = new FileOutputStream(propertyFileName)) {
            prop.setProperty(propertyName, value);
            prop.store(output, null);
        }
    }

    /**
     * Returns the property value.
     *
     * @param propertyName Property name.
     * @return Value of the property with given name or null if it does not
     * exist.
     */
    public String getProperty(String propertyName) {
        FileInputStream input = null;
        try {
            input = new FileInputStream(propertyFileName);
            prop.load(input);
            return prop.getProperty(propertyName);
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    return null;
                }
            }
        }
    }
}
